/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.uefs.cedoc.db.serie;

import br.uefs.cedoc.db.DBController;
import br.uefs.cedoc.db.exception.DBConnectionException;
import br.uefs.cedoc.entidades.SerieBean;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;


/**
 *
 * @author suporte
 */
public class DBSerie {

    public void criarSerie(SerieBean serie) throws SQLException, DBConnectionException {

        String comandoSQL = "INSERT INTO serie (serie) VALUES(?)";

        Connection conn = DBController.getInstance().getConnection();

        if (conn != null) {

            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
            prepStatement.setString(1, serie.getSerie());
            //DBOperacoes dbo = new DBOperacoes();
            //serie.setIdSerie(dbo.getMaxID("idserie", "serie"));	
            //System.out.println("IDSECAO= "+serie.getIdserie());
            //prepStatement.setInt(1, serie.getIdserie());
            prepStatement.execute();
		
            prepStatement.close();
        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conexao com banco ");
            dbException.printStackTrace();
            throw dbException;
        }




    }

    public synchronized void editarSerie(SerieBean serie) throws SQLException, DBConnectionException {

        String comandoSQL = "UPDATE serie SET serie = ? WHERE idserie = ?";

        Connection conn = DBController.getInstance().getConnection();
        //System.out.println("Editar acervo: "+acervo.getNome());
        if (conn != null) {

            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
            prepStatement.setString(1, serie.getSerie());
            prepStatement.setInt(2, serie.getIdSerie());
            prepStatement.execute();
            prepStatement.close();

        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
            dbException.printStackTrace();
            throw dbException;
        }


    }
    public SerieBean listaSerie(int id) throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM serie WHERE idserie=?"; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);

                prepStatement.setInt(1,id);
                ResultSet rs = prepStatement.executeQuery();



                while (rs.next()) {
                    SerieBean serie = new SerieBean();

                    serie.setIdSerie(rs.getInt("idserie"));
                    serie.setSerie(rs.getString("serie"));
                    
                    return serie;

                }
                DBController.getInstance().closeAll(conn, null, null);
               


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
            return null;
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    public SerieBean getSerie(String serie) throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM serie WHERE serie like ?"; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);

                prepStatement.setString(1,serie);
                System.out.println("MINHA SERIE="+"%"+serie);
                ResultSet rs = prepStatement.executeQuery();



                while (rs.next()) {
                    SerieBean serie_temp = new SerieBean();

                    serie_temp.setIdSerie(rs.getInt("idserie"));
                    serie_temp.setSerie(rs.getString("serie"));
                    
                    return serie_temp;

                }
                DBController.getInstance().closeAll(conn, null, null);
               


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
            return null;
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }



    	public  void removerSerie(int id) throws SQLException {

		String comandoSQL = "DELETE FROM serie WHERE idserie=?";

		Connection conn = DBController.getInstance().getConnection();
		
		if (conn != null){
			

			PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
			prepStatement.setInt(1, id);						
			prepStatement.execute();			
			prepStatement.close();
		}	

	}

    public SerieBean[] listarSeries() throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM serie "; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);


                ResultSet rs = prepStatement.executeQuery();

                Collection series = new ArrayList();

                while (rs.next()) {
                    SerieBean serie_temp = new SerieBean();

                    serie_temp.setIdSerie(rs.getInt("idserie"));
                    serie_temp.setSerie(rs.getString("serie"));
                    series.add(serie_temp);

                }
                DBController.getInstance().closeAll(conn, null, null);
                return (SerieBean[]) series.toArray(new SerieBean[0]);


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

